<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;

/**
 * 机构逻辑
 */
class Department extends LogicBase
{
    
    /**
     * 获取机构列表
     */
    public function getDepartmentList($where = [], $field = true, $order = '', $paginate = 0)
    {
        
        return $this->modelDepartment->getList($where, $field, $order, $paginate);
    }
    
    /**
     * 获取机构信息
     */
    public function getDepartmentInfo($where = [], $field = true)
    {

        return $this->modelDepartment->getInfo($where, $field);
    }



    
    /**
     * 机构编辑
     */
    public function departmentEdit($data = [],$url='')
    {
        
        $validate_result = $this->validateDepartment->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateDepartment->getError()];
        }
        
        $result = $this->modelDepartment->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '机构' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelHealthroom->getError()];
    }

    /**
     * 机构删除
     */
    public function departmentDel($where = [])
    {
        
        $result = $this->modelDepartment->deleteInfo($where);
        
        $result && action_log('删除', '机构删除' . '，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '删除成功'] : [RESULT_ERROR, $this->modelDepartment->getError()];
    }

     /**
     * 基层卫生医疗机构数据导入
     */
     public function healthcenterImport($test_url = 'd:\\healthcenter.xlsx')
     {

       $url = url('healthCommission/healthcenterList');
       $data = get_excel_data($test_url);


       $list=arrToList($data,['code','name','leader_code','leader_name','phone','nature','person_number','education','agreement_number','resident_number']);
            //定义变量跟踪多数据验证过程
       $is_validate_error=false;
       foreach($list as $rr){
        $validate_result = $this->validateDepartment->scene('import')->check($rr);
        if (!$validate_result) {
               // return [RESULT_ERROR, $this->validateDepartment->getError()];
            $is_validate_error=true;
        }
    }
    if(!$is_validate_error){
        $result= $this->modelDepartment->setList($list);
    }
    else
    {
        $result=false;
        $this->error='批量导入时数据验证失败，请检测数据后重试';
    }
       return $result ? [RESULT_SUCCESS, '导入成功',$url] : [RESULT_ERROR,$this->error];
}
    

    /**
     * 医院数据导入
     */
     public function hospitalImport($test_url = 'd:\\hospital.xlsx')
     {

       $url = url('healthCommission/hospitalList');
       $data = get_excel_data($test_url);


       $list=arrToList($data,['code','name','leader_code','leader_name','phone','nature','person_number','education','agreement_number','resident_number']);
            //定义变量跟踪多数据验证过程
       $is_validate_error=false;
       foreach($list as $rr){
        $validate_result = $this->validateDepartment->scene('import')->check($rr);
        if (!$validate_result) {
               // return [RESULT_ERROR, $this->validateDepartment->getError()];
            $is_validate_error=true;
        }
    }
    if(!$is_validate_error){
        $result= $this->modelDepartment->setList($list);
    }
    else
    {
        $result=false;
        $this->error='批量导入时数据验证失败，请检测数据后重试';
    }
       return $result ? [RESULT_SUCCESS, '导入成功',$url] : [RESULT_ERROR,$this->error];
}
}
